package com.cskaoyan.wordmemorize.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cskaoyan.wordmemorize.auth.SaAdminCheckPermission;
import com.cskaoyan.wordmemorize.auth.StpKit;
import com.cskaoyan.wordmemorize.common.exception.BusinessException;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dao.entity.EmployeeRoleDO;
import com.cskaoyan.wordmemorize.dao.mapper.EmployeeRoleMapper;
import com.cskaoyan.wordmemorize.dto.admin.DeleteEmployeeRoleOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.EmployeeDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.request.EmployeeCommand;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.service.EmployeeService;
import com.cskaoyan.wordmemorize.service.FileUploadService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/admin/employee")
public class AdminEmployeeController {

    @Autowired
    EmployeeService employeeService;

    @Autowired
    FileUploadService fileUploadService;

    @SaAdminCheckPermission("admin:employee:add")
    @PostMapping("")
    public Result addEmployee(@RequestBody EmployeeCommand employeeCommand){
        // 添加员工，电话、头像URL可能为空，其他都非空，但密码需要转换成密文
        // 添加员工时，任意增删role都不会触发请求，只有确定时才会一起发送；
        // 编辑时，删除已有的角色会直接发送请求，其他情况同增加。
        employeeService.addEmployee(employeeCommand);
        return Result.ok();
    }

    @SaAdminCheckPermission("admin:employee:update")
    @PutMapping("")
    public Result updateEmployee(@RequestBody EmployeeCommand employeeCommand){
        // 主要用于账号管理的员工权限管理，注意角色的变更，有可能新增，也有可能删除
        employeeService.updateEmployee(employeeCommand);
        return Result.ok();
    }

    @PostMapping("avatar")
    public Result<String> postAvatar(MultipartFile file) throws IOException {
        String upload = fileUploadService.upload(file);
        return Result.ok(upload);
    }

    @SaAdminCheckPermission("admin:employee:search")
    @GetMapping("list")
    public Result<PageDTO<EmployeeDTO>> getEmployeeList(PageRequest pageRequest){
        PageDTO<EmployeeDTO> employeePage = employeeService.getEmployeePage(pageRequest);
        return Result.ok(employeePage);
    }

    @GetMapping("info")
    public Result<EmployeeDTO> getCurrentLoginEmployeeInfo(){
        // 这个接口是在用户登录后自动调用，获得当前登录用户的信息以及权限，还会获得权限码，根据权限码显式不同的内容
        // 因为采用两个账号体系，不能使用StpUtil，需要使用自定义的StpKit
        long loginId = StpKit.ADMIN.getLoginIdAsLong();
        EmployeeDTO employeeInfo = employeeService.getEmployeeInfo(loginId);
        return Result.ok(employeeInfo);
    }

    @SaAdminCheckPermission("admin:employee:delete")
    @DeleteMapping("{employeeId}")
    public Result deleteEmployee(@PathVariable("employeeId") Long employeeId){
        employeeService.delete(employeeId);
        return Result.ok();
    }

    @DeleteMapping("{employeeId}/role/{roleId}")
    public Result deleteEmployeeRoleRelation(@PathVariable("employeeId")Long employeeId, @PathVariable("roleId")Long roleId){
        employeeService.deleteRole(employeeId, roleId);
        return Result.ok();
    }

}
